package com.jianguozh.difs.support.service.sys.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianguozh.difs.support.service.sys.entity.Res;
import com.jianguozh.difs.support.service.sys.entity.Role;
import com.jianguozh.difs.support.service.sys.entity.User;
import com.jianguozh.difs.support.service.sys.mapper.UserMapper;
import com.jianguozh.difs.support.service.sys.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author hanrs
 * @since 2022-03-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User getByUsername(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }

    @Override
    public Set<String> existUsername() {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(User.class, e -> "username".equalsIgnoreCase(e.getColumn()));
        return getBaseMapper().selectObjs(lambdaQueryWrapper).stream().map(o -> (String) o).collect(Collectors.toSet());
    }

    @Override
    public List<Role> listUserRoles(String userId) {
        return getBaseMapper().selectUserRole(userId);
    }

    @Override
    public List<Res> listUserClientRes(String userId, String clientId) {
        return getBaseMapper().selectListUserClientRes(userId, clientId);
    }

    @Override
    public List<Tree<String>> treeListUserClientRes(String userId, String clientId) {
        List<Res> resList = listUserClientRes(userId, clientId);
        TreeNodeConfig config = new TreeNodeConfig();
        config.setWeightKey("sort").setParentIdKey("parentId").setIdKey("id").setChildrenKey("children");
        List<Tree<String>> trees = TreeUtil.build(resList, null, config, (obj, tree) -> {
            tree.setParentId(obj.getParentId());
            tree.setId(obj.getId());
            tree.putExtra("key", obj.getId());
            tree.putExtra("parentKey", obj.getParentId());
            tree.putExtra("sort", obj.getSort());
            tree.putExtra("name", obj.getResName());
            tree.putExtra("badge", obj.getBadge());
            tree.putExtra("icon", obj.getIcon());
            tree.putExtra("path", obj.getPath());
            tree.putExtra("component", obj.getComponent());
            tree.putExtra("permission", obj.getPermission());
        });
        return trees;
    }


    @Override
    public User getDefaultManager(String orgId) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getOrgId, orgId).eq(User::getOrgDefault, true);
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }

    @Override
    @Transactional
    public boolean saveOrUpdateWithRole(User entity, List<String> roleIds) {
        boolean result = super.saveOrUpdate(entity);
        String userId = entity.getId();
        getBaseMapper().deleteUserRole(userId);
        getBaseMapper().insertUserRole(userId, roleIds);
        return result;
    }

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        getBaseMapper().deleteUserRole(id);
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean removeByIds(Collection<?> list) {
        getBaseMapper().deleteBatchUserRole(list);
        return super.removeByIds(list);
    }

}
